<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Leave_management extends CI_Controller {
 
	function __construct()
	{
		parent::__construct();
		
		// check if user logged in 
		if (!$this->ion_auth->logged_in())
	  	{
			redirect('auth/login');
	  	}elseif(!$this->ion_auth->is_admin($this->session->userdata('user_id'))){
			redirect('auth/login');
		}		
		$this->load->library('form_validation');
		$this->load->model('founder/leave_management_model');
		$this->load->model('user/leave_model');
		$this->load->model('user/attendance_model');
		
	}

function index()
   {
	   	$data['all_leave'] = $this->leave_management_model->get_all_leave();
	   	$meta['page_title'] = "Leave Management";	   
	  	$this->load->view('common/header',$meta);
		$this->load->view('founder/leave-management',$data);
		$this->load->view('common/footer');
   }
   
   function edit_leave_status($id,$user_id=NULL)
   {
   		$data['id'] = $id;
	 	$data['user'] = $this->leave_management_model->get_leave_by_id($id);
		//calculating the allocated leave for this user
		$data['total_paid_leaves'] = $this->leave_management_model->total_paid_leaves($user_id);
		
		//calculating the leave taken for this user
		$data['paid_leaves_taken'] = $this->leave_management_model->paid_leaves_taken($user_id);
		
		//calculating the allocated leave		
		$data['allocated_leave'] = $data['total_paid_leaves']['paid_leave'] - $data['paid_leaves_taken']['pl'];		
		
	 	$this->load->view('founder/leave-status',$data);		
   }

	function grant_leave($id)
	{
		$update = $this->input->post();
				
		$this->leave_management_model->grant_leave($update,$id);		
		redirect('founder/leave_management');	
		
	}
	
}
	
?>